package com.frame.ar.model;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.frame.ar.db.QueryParamsAnd;
import com.frame.ar.kit.ObjKit;
import com.frame.config.ParamsConfig;
import com.frame.hander.PagerHander.Pager;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/*******************************************
 * 作用描述：BJUI框架格式的page数据格式
 ******************************************* 
 * @author LRC
 * @createDate 2015-9-13
 * @version v1.0
 * @updateRecord 格式：时间，作者，修改说明
 ******************************************* 
 * 
 */
/*******************************************
 * 作用描述：
 *******************************************
 * @author LRC
 * @createDate 2015-9-19
 * @version v1.0 
 * @updateRecord 格式：时间，作者，修改说明
 *******************************************
 * 
 */
public class BJUIPager {
	
	/**
	 * 分页查询当前页
	 */
	private Integer pageCurrent;
	/**
	 * 页容量
	 */
	private Integer pageSize;
	/**
	 * 排序字段
	 */
	private String orderField;
	/**
	 * 排序方向
	 */
	private String orderDirection;
	/**
	 * 返回数据对象
	 */
	private PageData pageData;
	/**
	 * 查询参数封装
	 */
	private QueryParamsAnd qr;
	/**
	 * 请求传入参数封装
	 */
	private Map<String,Object> params;
	
	/**
	 * 分页页脚参数
	 */
	private PageFooter footer;
	/**
	 * 保留参数
	 */
	private static Set<String> keepP = new HashSet<String>();
	static{
		keepP.add("pageCurrent");
		keepP.add("pageSize");
		keepP.add("orderField");
		keepP.add("orderDirection");
	}
	/*******************************************
	 * 功能描述：从请求参数中初始化分页查询组件
	 *******************************************
	 * @createDate 2015-9-15 by LRC
	 * @param c jfinal的请求控制器
	 * @param defaultExtWhenNull 当参数的operator(即过滤表达式)为null时默认为
	 * @param except 列外参数列表，使用,分开
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public void initQueryParams(Controller c,String defaultExtWhenNull,String... except){
		Set<String> _keepP = new HashSet<String>();
		for(String e : except){
			_keepP.add(e);
		}
		//缺省值为Like
		defaultExtWhenNull = ObjKit.isNull(defaultExtWhenNull) ? "like" : defaultExtWhenNull;
		params = new HashMap<String,Object>();
		pageCurrent = c.getParaToInt("pageCurrent");
		pageSize = c.getParaToInt("pageSize");
		orderField = c.getPara("orderField");
		orderDirection = c.getPara("orderDirection");
		//修正参数
		if (pageCurrent == null || pageCurrent < 1) {
			pageCurrent = 1;
		}
		if (pageSize == null || pageSize < 1) {
			pageSize = Integer.parseInt(ParamsConfig.getInstance().getValue("Page.SIZE"));
		}
		//从解析传入的参数
		Enumeration<String> names = c.getParaNames();
		qr = new QueryParamsAnd();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			if(_keepP.contains(name) || keepP.contains(name) || name.indexOf(".")>=0)continue;
			//设置参数
			Object value = c.getPara(name);
			Object _value = value;
			if(ObjKit.isNull(_value))continue;
			String _expression = c.getPara(name+".operator");
			if(_expression==null)_expression=defaultExtWhenNull;
			switch(_expression){
				case "like":_value= "%"+_value+"%";break;
			}
			qr.put(name, _expression, _value);
			params.put(name, value);
		}
		//设置排序规则
		if(orderField!=null){
			qr.setOrder(orderField + " " + orderDirection);
		}
	}
	/*******************************************
	 * 功能描述：设置page返回数据
	 *******************************************
	 * @createDate 2015-9-13 by LRC
	 * @param total
	 * @param list 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	private void setPageData(Integer total,List list){
		if(pageData==null)pageData = new PageData();
		pageData.total= total==null || total < 0 ? 0 : total;
		pageData.pageCurrent = pageCurrent==null ? 1 : pageCurrent;
		pageData.list = list == null ? new ArrayList(): list;
	}
	//设置默认值
	public void setOrderDefaultValue(String order){
		if(orderField==null||"".equals(orderField)){
			qr.setOrder(order);
		}
	}
	
	public class PageData {
		private Integer total;
		private Integer pageCurrent;
		private List list;

		public Integer getTotal() {
			return total;
		}

		public void setTotal(Integer total) {
			this.total = total;
		}

		public Integer getPageCurrent() {
			return pageCurrent;
		}

		public void setPageCurrent(Integer pageCurrent) {
			this.pageCurrent = pageCurrent;
		}

		public List getList() {
			return list;
		}

		public void setList(List list) {
			this.list = list;
		}
	}
	public Integer getPageCurrent() {
		return pageCurrent;
	}
	public void setPageCurrent(Integer pageCurrent) {
		this.pageCurrent = pageCurrent;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public String getOrderField() {
		return orderField==null ? "":orderField;
	}
	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}
	public String getOrderDirection() {
		return orderDirection==null ? "":orderDirection;
	}
	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}
	public PageData getPageData() {
		return pageData;
	}
	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}
	public QueryParamsAnd getQr() {
		return qr;
	}
	public void setQr(QueryParamsAnd qr) {
		this.qr = qr;
	}
	public Map<String, Object> getParams() {
		return params;
	}
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	
	/*******************************************
	 * 作用描述：分页查询页脚参数
	 *******************************************
	 * @author LRC
	 * @createDate 2015-9-19
	 * @version v1.0 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public static class PageFooter{
		public boolean hasPre = true;
		public boolean hasNext = true;
		public int rowStart = 0;
		public int start = 1;
		public int end = 10;
		public long count;
		public int totalPage;
		public int pageSize;
		public int pageIndex;
		public boolean isHasPre() {
			return hasPre;
		}
		public void setHasPre(boolean hasPre) {
			this.hasPre = hasPre;
		}
		public boolean isHasNext() {
			return hasNext;
		}
		public void setHasNext(boolean hasNext) {
			this.hasNext = hasNext;
		}
		public int getStart() {
			return start;
		}
		public void setStart(int start) {
			this.start = start;
		}
		public int getEnd() {
			return end;
		}
		public void setEnd(int end) {
			this.end = end;
		}
		public long getCount() {
			return count;
		}
		public void setCount(long count) {
			this.count = count;
		}
		public int getTotalPage() {
			return totalPage;
		}
		public void setTotalPage(int totalPage) {
			this.totalPage = totalPage;
		}
		public int getPageSize() {
			return pageSize;
		}
		public void setPageSize(int pageSize) {
			this.pageSize = pageSize;
		}
		public int getPageIndex() {
			return pageIndex;
		}
		public void setPageIndex(int pageIndex) {
			this.pageIndex = pageIndex;
		}
		public int getRowStart() {
			return rowStart;
		}
		public void setRowStart(int rowStart) {
			this.rowStart = rowStart;
		}
	}
	
	private void initFooter(Page<Record> page,int showPagerNum){
		footer = new PageFooter();
		footer.pageIndex = page.getPageNumber();
		footer.pageSize = page.getPageSize();
		footer.totalPage = page.getTotalPage();
		footer.count = page.getTotalRow();
		footer.rowStart = (footer.pageIndex-1)*footer.pageSize;
		if(footer.pageIndex<=1){
			footer.hasPre = false;
		}
		if(footer.pageIndex>=footer.totalPage){
			footer.hasNext = false;
		}
		if(showPagerNum<=0){
			showPagerNum = 5;
		}
		//计算起始页
		if(footer.totalPage<=showPagerNum){
			//如果总页数小于要显示的页码数，则显示全部
			footer.start = 1;
			footer.end = footer.totalPage;
		}else{
			//计算如果从pageIndex起，最后要显示的页面
			int l = showPagerNum + footer.pageIndex-1;
			if(l<=footer.totalPage){
				footer.start = footer.pageIndex;
				footer.end = l;
			}else{
				footer.start = footer.totalPage-showPagerNum+1;
				footer.end = footer.totalPage;
			}
		}
	}
	public PageFooter getFooter() {
		return footer;
	}
	public void setFooter(PageFooter footer) {
		this.footer = footer;
	}
	/*******************************************
	 * 功能描述：填充数据参数
	 *******************************************
	 * @createDate 2015-9-19 by LRC
	 * @param users 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public void fillPageData(Page<Record> page,Integer showPagerNum) {
		if(page==null)return;
		initFooter(page, showPagerNum==null ? 5:showPagerNum);
		setPageData(page.getTotalRow(), page.getList());
	} 
}
